package com.example.commuity.adapter

import android.content.Intent
import android.view.View
import androidx.databinding.DataBindingUtil
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.common.RouterPath
import com.example.common.utils.DateTimeUtils
import com.example.commuity.databinding.ItemAttentionCardViewBinding
import com.example.commuity.entity.viewdata.AttentionFollowViewData
import com.example.entity.BaseEntity
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack

/**
 * 社区关注列表适配器
 * */
class AttentionListAdapter constructor(layoutId: Int) :
    BaseQuickAdapter<BaseEntity, BaseViewHolder>(layoutId) {

    override fun onItemViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        DataBindingUtil.bind<ItemAttentionCardViewBinding>(viewHolder.itemView)
    }

    override fun convert(holder: BaseViewHolder, item: BaseEntity) {
        var dataBinding = holder.getBinding<ItemAttentionCardViewBinding>()

        if (item != null && dataBinding != null) {
            var entity = item as AttentionFollowViewData
               //加载封面图片
                dataBinding.videoItemPlayer.setCoverImage(entity.coverUrl, 0)
                //封装GSYPlayer的参数
                var gsyVideoOptionBuilder = GSYVideoOptionBuilder()
                    gsyVideoOptionBuilder.setUrl(entity.playUrl)
                        .setVideoTitle(entity.title)
                        .setIsTouchWiget(false)
                        .setCacheWithPlay(false)
                        .setRotateViewAuto(true)
                        .setLockLand(true)
                        .setPlayTag("playTag")
                        .setShowFullAnimation(true)
                        .setNeedLockFull(true)
                        .setPlayPosition(holder.adapterPosition)
                    .setVideoAllCallBack(object : GSYSampleCallBack() {
                        override fun onPrepared(url: String?, vararg objects: Any?) {
                            super.onPrepared(url, *objects)
                            if (!dataBinding.videoItemPlayer.isIfCurrentIsFullscreen) {
                                GSYVideoManager.instance().isNeedMute = true
                            }
                        }

                        override fun onEnterFullscreen(url: String?, vararg objects: Any?) {
                            super.onEnterFullscreen(url, *objects)
                            GSYVideoManager.instance().isNeedMute = false
                            dataBinding.videoItemPlayer.currentPlayer.titleTextView.text = objects[0].toString()
                            dataBinding.videoItemPlayer.backButton.visibility = View.VISIBLE
                        }

                        override fun onQuitFullscreen(url: String?, vararg objects: Any?) {
                            super.onQuitFullscreen(url, *objects)
                            GSYVideoManager.instance().isNeedMute = true
                            dataBinding.videoItemPlayer.backButton.visibility = View.GONE
                            dataBinding.videoItemPlayer.currentPlayer.titleTextView.visibility = View.GONE
                        }

                        override fun onEnterSmallWidget(url: String?, vararg objects: Any?) {
                            super.onEnterSmallWidget(url, *objects)
                        }

                    })

                 gsyVideoOptionBuilder.build(dataBinding.videoItemPlayer)

                //点击视频进入到详情
                dataBinding.videoItemPlayer.rvContent.setOnClickListener {
                    //点击进入详情页面
                    ARouter.getInstance().build(RouterPath.PATH_ACTIVITY_VIDEO)
                        .withString("playerUrl",entity.playUrl).navigation()


                }

                dataBinding.videoItemPlayer.titleTextView.visibility = View.GONE
                dataBinding.videoItemPlayer.backButton.visibility = View.GONE
                //设置视频时长
                dataBinding.tvReleaseTime.text = DateTimeUtils.getDate(entity.releaseTime.toString(), "HH:mm")


            //绑定其他Item数据
            dataBinding.viewData = entity
            dataBinding.executePendingBindings()
        }
    }
}